package com.lt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lt.reggie.common.BaseContext;
import com.lt.reggie.common.R;
import com.lt.reggie.entity.ShoppingCart;
import com.lt.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //设置当前用户id,指定是哪个用户的购物车
        Long shoppingCartId = BaseContext.getThreadLocal();
        shoppingCart.setUserId(shoppingCartId);
        //判断当前添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //判断dishId是否存在
        if (dishId != null) {
            //存在，是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        } else {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询当前菜品或套餐是否已在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        //判断当前菜品是否在购物车中
        if (cartServiceOne != null) {
            //存在，在原来的基础上数量加1
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //不存在，向购物车中加入菜品或套餐
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

            return R.success(cartServiceOne);
    }

    /**
     * 根据用户id查询购物车信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车~~");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    /**
     * 菜品或套餐数量减一
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){

        //设置当前用户id,指定是哪个用户的购物车
        Long shoppingCartId = BaseContext.getThreadLocal();
        shoppingCart.setUserId(shoppingCartId);
        //判断当前添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //判断dishId是否存在
        if (dishId != null) {
            //存在，是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        } else {
            //是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //执行查询
        ShoppingCart shoppingCartOne  = shoppingCartService.getOne(queryWrapper);
        //将数量减一
        shoppingCartOne.setNumber(shoppingCartOne.getNumber() - 1);
        //判断当前菜品或套餐的数量是否为零
        if (shoppingCartOne.getNumber() == 0) {
            //为 0
            shoppingCartService.removeById(shoppingCartOne.getId());
        } else {
            //不为零
            //对购物车进行更新
            shoppingCartService.updateById(shoppingCartOne);
        }

        return R.success(shoppingCartOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
       /* LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getThreadLocal());
        shoppingCartService.remove(queryWrapper);*/
        boolean sign = shoppingCartService.clean();
        return sign ? R.success("删除成功") : R.error("删除失败");
    }
}
